<?php
/**
 * 后台入口控制器
 *
 * - 后台框架，顶部导航菜单
 *
 * @author smallmenu
 * @version
 */
class IndexAction extends AbstractAction
{
	/**
	 * @var MenuModel
	 */
    private $Menu = NULL;

	/**
	 * 控制器初始化
	 *
	 * @return NULL
	 */
    protected function _initialize()
    {
		parent::_initialize();
        $this->Menu = D('Admin/Menu');
    }

    /**
     * 后台框架
	 *
	 * @return HTML
     */
	public function index()
	{
		$submenus = array();
		$topmenus = $this->Menu->getChild(NULL);

		foreach ($topmenus as $k => $r) {
			if ($r['childids']) {
				$array = $this->Menu->getChild($r['menuid']);
				$submenus[$r['menuid']] = $array;
			}
		}

		$this->assign('topmenus', $topmenus);
		$this->assign('submenus', $submenus);
		$this->display();
	}

    /**
     * 菜单数据输出
	 *
	 * @return JSON
     */
	public function menu()
	{
		$parentid = $_GET['root'];
		$menus = array();

		$array = $this->Menu->getChild($parentid);
		foreach ($array as $k => &$r) {
			$r['id'] = $r['menuid'];
			$r['expanded'] = false;
			$r['hasChildren'] = is_null($r['childids']) ? false : true;
		}

		foreach ($array as &$r) {
			if (is_null($r['childids'])) {
				$menus[] = array('id' => $r['id'], 'text' => $r['name'], 'url' => $r['url']);
			}
			else {
				$menus[] = array('id' => $r['id'], 'text' => $r['name'], 'url' => $r['url'], 'expanded' => false, 'hasChildren' => true);
			}
		}
		echo json_encode($menus);
	}
}